package com.cykj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cykj.common.core.domain.R;
import com.cykj.common.core.utils.StringUtils;
import com.cykj.common.core.utils.file.FileUtils;
import com.cykj.file.utils.FileUploadUtils;
import com.cykj.user.domain.Customer;
import com.cykj.user.mapper.CustomerMapper;
import com.cykj.user.service.IUserInfoService;
import com.cykj.user.utils.SensitiveWordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private SensitiveWordUtils sensitiveWordUtils;

    @Override
    public R<Customer> getUserInfo(Integer userId) {
        Customer customer = customerMapper.selectById(userId);
        return R.ok(customer);
    }
    @Override
    public R<String> updateBlacklistStatus(Integer userId, Integer isBlacklisted) {
        // 校验参数
        if (userId == null || (isBlacklisted != 0 && isBlacklisted != 1)) {
            return R.fail("参数错误");
        }
        // 校验用户是否存在
        Customer customer = customerMapper.selectById(userId);
        if (customer == null) {
            return R.fail("用户不存在");
        }
        // 更新拉黑状态
        int result = customerMapper.updateBlacklistStatus(userId, isBlacklisted);
        return result > 0
                ? R.ok(isBlacklisted == 1 ? "用户已加入黑名单" : "用户已移出黑名单")
                : R.fail("操作失败");
    }



    @Override
    public R<String> updateUsername(Integer userId, String name) {
        // 敏感词校验
        String errorMsg = sensitiveWordUtils.validateUsername(name);
        if (errorMsg != null) {
            return R.fail(errorMsg);
        }
        LambdaUpdateWrapper<Customer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Customer::getId, userId)
                .set(Customer::getName, name);

        int result = customerMapper.update(null, updateWrapper);
        return result > 0 ? R.ok("用户名更新成功") : R.fail("用户名更新失败");
    }

    @Override
    public R<String> deleteUser(String userId) {
        int result = customerMapper.deleteById(userId);
        return result > 0 ? R.ok("删除成功") : R.fail("删除失败");
    }


    @Override
    public R<String> updateBalance(String userId, BigDecimal amount) {
        Customer customer = customerMapper.selectById(userId);
        if (customer == null) {
            return R.fail("用户不存在");
        }
        
        BigDecimal newBalance = customer.getBalance().add(amount);
        if (newBalance.compareTo(BigDecimal.ZERO) < 0) {
            return R.fail("余额不足");
        }
        
        LambdaUpdateWrapper<Customer> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Customer::getId, userId)
                    .set(Customer::getBalance, newBalance);
        int result = customerMapper.update(null, updateWrapper);
        
        return result > 0 ? R.ok("余额更新成功") : R.fail("余额更新失败");
    }


}
